// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Represents the database column that a property is mapped to.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class SqlColumnAttribute : Attribute, ISqlColumn
    {
        /// <summary>
        ///     Initializes a new instance of the SqlColumnAttribute class.
        /// </summary>
        /// <param name="column">The name of the column which the property is mapped to.</param>
        public SqlColumnAttribute(string column)
        {
            this.Column = column;
            this.Order = -1;
            Check.NotEmpty(column, nameof(column));
        }

        /// <summary>
        ///     Initializes a new instance of the SqlColumnAttribute class.
        /// </summary>
        /// <param name="tableAlias">
        ///     The alias of the table where the column belongs to. This alias must be the same as 
        ///     the Alias property of the FromTable attribute or the JoinTable) attribute of the model class.
        /// </param>
        /// <param name="column">The name of the column which the property is mapped to.</param>
        public SqlColumnAttribute(string tableAlias, string column)
        {
            this.TableAlias = tableAlias;
            this.Column = column;
            this.Order = -1;

            Check.NotEmpty(tableAlias, nameof(tableAlias));
            Check.NotEmpty(column, nameof(column));
        }

        /// <summary>
        ///     Initializes a new instance of the SqlColumnAttribute class.
        /// </summary>
        /// <param name="tableAlias">
        ///     The alias of the table where the column belongs to. This alias must be the same as 
        ///     the Alias property of the FromTable attribute or the JoinTable) attribute of the model class.
        /// </param>
        /// <param name="column">The name of the column which the property is mapped to.</param>
        /// <param name="columnAlias">The alias of the column which the property is mapped to.</param>
        public SqlColumnAttribute(string tableAlias, string column, string columnAlias)
        {
            this.TableAlias = tableAlias;
            this.Column = column;
            this.ColumnAlias = columnAlias;
            this.Order = -1;

            Check.NotEmpty(tableAlias, nameof(tableAlias));
            Check.NotEmpty(column, nameof(column));
            Check.NotEmpty(columnAlias, nameof(columnAlias));
        }

        /// <summary>
        ///     Gets the alias of the table where the column belongs to.
        /// </summary>
        public virtual string TableAlias { get; }

        /// <summary>
        ///     Gets the name of the column which the property is mapped to.
        /// </summary>
        public virtual string Column { get; protected set; }

        /// <summary>
        ///     Gets the alias of the column which the property is mapped to.
        /// </summary>
        public virtual string ColumnAlias { get; set; }

        /// <summary>
        ///     Gets whether the table (where the column belongs to) has an alias.
        /// </summary>
        public bool TableAliasDefined => !String.IsNullOrWhiteSpace(this.TableAlias);

        /// <summary>
        ///     Gets whether the column has an alias.
        /// </summary>
        public bool ColumnAliasDefined => !String.IsNullOrWhiteSpace(this.ColumnAlias);

        /// <summary>
        ///     Gets or sets the zero-based order of the column which the property is mapped to.
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        ///     Gets or sets the data type (specific to the database provider) of the
        ///     column which the property is mapped to.
        /// </summary>
        public string TypeName { get; set; }

        public bool TypeNameDefined => !String.IsNullOrWhiteSpace(this.TypeName);
    }
}
